package org.example.blog_manage.interceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.blog_manage.utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入阻拦状态...");

//        从header中获取token
        String token = request.getHeader("user_token_key");
//        验证token
        Claims claims = JwtUtils.parseToken(token);
        log.info("验证结果：{}",claims);
        if(claims==null){
//          设置状态
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
